package com.zhihaoliang.map.util

import coil.intercept.Interceptor
import coil.request.ImageResult
import okhttp3.HttpUrl.Companion.toHttpUrl


/**
 * 图片加载的适配器
 */
object UnsplashSizingInterceptor:Interceptor{
    override suspend fun intercept(chain: Interceptor.Chain): ImageResult {
       val data = chain.request.data
        val size = chain.size
        if(data is String && data.startsWith("https://images.unsplash.com/photo-")){
            val url = data.toHttpUrl().newBuilder().addQueryParameter("w",size.width.toString())
                .addQueryParameter("h",size.height.toString()).build()

            val request = chain.request.newBuilder().data(url).build()
            return chain.proceed(request)
        }

        return chain.proceed(chain.request)
    }

}